package com.inc.admin.controller.biz;

import com.inc.admin.domain.biz.BookHistory;
import com.inc.admin.exception.SysCode;
import com.inc.admin.service.biz.BookHistoryService;
import com.inc.admin.utils.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/bookHistory")
public class BookHistoryController {
    @Resource
    private BookHistoryService bookHistoryService;

    /**
     * 分页查询
     */
    @PostMapping("/listHistoryByPage")
    public R listHistoryByPage(@RequestBody BookHistory req) {
        return R.ok().put("page", bookHistoryService.listHistoryByPage(req));
    }

    /**
     * 添加
     */
    @PostMapping("/insertHistory")
    public R insertHistory(@RequestBody BookHistory req) {
        int count = bookHistoryService.insertHistory(req);
        if(count==-1){
            return R.error("500","库存不足");
        }else{
            return R.data(count);
        }
    }

    /**
     * 更新
     */
    @PostMapping("/updateHistory")
    public R updateHistory(@RequestBody BookHistory req) {
        return R.operate(bookHistoryService.updateHistory(req)>0);
    }

    /**
     * 删除
     */
    @PostMapping("/deleteHistory")
    public R deleteHistory(@Validated @NotNull(message = "编号不能为空") @RequestParam("id") @RequestBody Integer id) {
        return R.operate(bookHistoryService.deleteHistory(id)>0);
    }
}
